package leetron.asynctask;

import android.R;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MyAsyncTask extends AsyncTask<Void, Integer, Void> {

	Activity contextCha;

	public MyAsyncTask(Activity contextCha) {
		super();
		this.contextCha = contextCha;
	}

	// ham nay thuc hien dau tien
	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		Toast.makeText(contextCha, "OnPreExecute", Toast.LENGTH_LONG).show();
	}

	// sau do toi ham doInBackground
	@Override
	protected Void doInBackground(Void... params) {
		for (int i = 0; i <= 100; i++) {
			// sleep 100 mili giay
			SystemClock.sleep(100);
			publishProgress(i);
		}
		return null;
	}

	// cap nhat giao dien tai ham nay

	@Override
	protected void onProgressUpdate(Integer... values) {
		// TODO Auto-generated method stub
		super.onProgressUpdate(values);
		ProgressBar paCha = (ProgressBar) contextCha
				.findViewById(leetron.asynctask.R.id.progressBar1);
		int giatri = values[0];

		paCha.setProgress(giatri);

		TextView txtmsg = (TextView) contextCha
				.findViewById(leetron.asynctask.R.id.textView1);
		txtmsg.setText(giatri + "%");
	}

	// sau khi tien trinh thuc hien thi ham nay xay ra

	@Override
	protected void onPostExecute(Void result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		Toast.makeText(contextCha, "Update da xong.", Toast.LENGTH_LONG).show();
	}
	/*
	public void goividu(){
		viduParamschoham();
		viduParamschoham(5);
		viduParamschoham(5,6);
		viduParamschoham(5,6,5,6,7,8,9,0,0);
	}
	
	public void viduParamschoham(int ... ds){
		int pt0 = ds[0];
		for(int n:ds){
			System.out.println(n);
		}
		for(int i =0; i < ds.length; i++){
			System.out.println(ds[i]);
		}
	}*/

}
